/*
 * @Author: XiaoFeng Chen 
 * @Date: 2018-09-27 10:02:33 
 * @Last Modified by: XiaoFeng Chen
 * @Last Modified time: 2018-11-15 12:02:07
 * 元素处理相关的工具库
 */

/**
 * 获取元素的最小尺寸（宽高）。
 * @param {Element} el  元素的宽高必须是固定的
 * @returns {Object} width、height
 */
export var getMinSize = function (el) {
    var div = document.createElement("div");
    div.style["overflow"] = "scroll";
    div.style["width"] = "0px";
    div.style["height"] = "0px";
    div.appendChild(el.cloneNode(true));
     //firefox 滚动宽度和chrome scrollWidth 不一致。
    document.body.appendChild(div);
    var minWidth = div.scrollWidth;
    var minHeight = div.scrollHeight;
    document.body.removeChild(div);
    return {
        width:minWidth,
        height:minHeight
    };
}
/**
 * 获取元素对象的位置
 * @param {Element} el 元素
 * @returns {Object} top、left
 */
export var getPosition = function (el) {
    // console.log(el.getBoundingClientRect());
    var top = el.offsetTop,
      left = el.offsetLeft;
    while (el = el.offsetParent) {
      top += el.offsetTop;
      left += el.offsetLeft;
    }
    return {
      top,
      left
    }
  }
  var _closest=Element.prototype.closest;
  
  /**
   * 寻找最近的元素
   * @param {Element} el 元素
   * @param {String} selector 选择器
   * @returns {Element|null}
   */
  export var closest = function(el,selector){
      if(_closest){
        return _closest.call(el,selector)
      }
      var matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
      
      while (el) {
          if (matchesSelector.call(el, selector)) {
              break;
          }
          el = el.parentElement;
      }
      return el;
  }